<template>
  <el-dialog
    v-model="visible"
    :title="`${paramsProps.title}`"
    :destroy-on-close="true"
    width="580px"
    draggable
    append-to-body
  >
    <el-form
      ref="ruleFormRef"
      label-width="140px"
      label-suffix=" :"
      :rules="rules"
      :model="paramsProps.row"
      @submit.enter.prevent="handleSubmit"
    >
      <el-form-item label="上级部门" prop="pid">
        <el-tree-select
          v-model.number="paramsProps.row.pid"
          :data="parentDeptTree"
          check-strictly
          placeholder="请选择上级"
          :render-after-expand="false"
          clearable
          :default-expand-all="true"
          :props="treeProps"
        />
      </el-form-item>
      <el-form-item label="部门名称" prop="deptName">
        <el-input
          v-model="paramsProps.row.deptName"
          placeholder="请填写部门名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="负责人" prop="leader">
        <el-input v-model="paramsProps.row.leader" placeholder="请填写负责人" clearable></el-input>
      </el-form-item>
      <el-form-item label="手机号码" prop="mobile">
        <el-input
          v-model="paramsProps.row.mobile"
          placeholder="请填写负责人手机号码"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input
          v-model="paramsProps.row.email"
          placeholder="请填写负责人邮箱"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="deptSort">
        <el-input-number v-model="paramsProps.row.deptSort" :precision="0" :min="1" :max="999999" />
      </el-form-item>
      <el-form-item label="是否停用" prop="isLock">
        <el-radio-group v-model="paramsProps.row.isLock">
          <el-radio value="T" border>是</el-radio>
          <el-radio value="F" border>否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="paramsProps.row.remark" placeholder="请填写备注" clearable></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false"> 取消</el-button>
      <el-button type="primary" @click="handleSubmit"> 确定</el-button>
    </template>
  </el-dialog>

  <DeptLeaderTransfer ref="deptLeaderTransferRef" v-model="selectLeaderIds" />
</template>

<script setup lang="ts">
import { ref, reactive, computed } from 'vue'
import { type ElForm, ElMessage } from 'element-plus'
import type { ISysDept } from '@/api/interface/system/sysDept'
import { getDeptTree } from '@/api/modules/system/sysDept'

defineOptions({
  name: 'SysDeptForm'
})
const parentDeptTree = ref<ISysDept.Tree[]>([])
const treeProps = {
  label: 'deptName',
  value: 'id'
}


const selectLeaderIds = ref<number[]>([])

// 接收父组件传过来的参数
const acceptParams = (params: View.DefaultParams) => {
  paramsProps.value = params
  selectLeaderIds.value = paramsProps.value.row?.leaders || []
  visible.value = true
  loadParentDept()
}

const loadParentDept = () => {
  getDeptTree({ excludeNodeId: paramsProps.value.row?.id }).then((res) => {
    parentDeptTree.value = res.data
  })
}

const rules = reactive({
  deptName: [{ required: true, message: '请填写部门名称' }],
  pid: [{ required: true, message: '请选择上级部门' }],
  isLock: [{ required: true, message: '请填写是否锁定' }],
  delFlag: [{ required: true, message: '请填写删除标识' }]
})

const visible = ref(false)
const paramsProps = ref<View.DefaultParams>({
  title: '',
  row: {},
  api: undefined,
  getTableList: undefined
})

// 提交数据（新增/编辑）
const ruleFormRef = ref<InstanceType<typeof ElForm>>()
const handleSubmit = () => {
  ruleFormRef.value!.validate(async (valid) => {
    if (!valid) return
    try {
      paramsProps.value.row.leaders = selectLeaderIds.value
      await paramsProps.value.api!(paramsProps.value.row)
      ElMessage.success({ message: `${paramsProps.value.title}成功！` })
      paramsProps.value.getTableList!()
      visible.value = false
    } catch (error) {
      console.log(error)
    }
  })
}

defineExpose({
  acceptParams
})
</script>

<style scoped lang="scss">
.user-item {
  margin: 5px;
}
</style>
